package cn.sqlog.simplewol.base;

import cn.sqlog.simplewol.base.entity.SymbolPlus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 通用方法
 * @author SQ
 */
public class BaseController<E,S> {
    @Autowired
    public  S sService;
    /**
     * 通用添加方法
     * @param entity 添加对象
     */
    public boolean add(E entity){
       return ((IService) sService).save(entity);
    }

    /**
     * 根据主键删除方法
     * @param id 主键id
     */
    public boolean del(Integer id){
       return ((IService) sService).removeById(id);
    }




    /**
     * 更新方法 根据主键
     * @param entity 更改后的对象
     */
    public boolean update(E entity ){
        boolean b = ((IService) sService).updateById(entity);
        return b;
    }
    public boolean update(UpdateWrapper<E> updateWrapper ){
        boolean b = ((IService) sService).update(updateWrapper);
        return b;
    }
    /**
     * 根据主键更新
     * @param entityList
     * @return
     */
    public boolean base_list_update_by_id(List<E> entityList){
        boolean b = ((IService) sService).updateBatchById(entityList);
        return b;
    }

    /**
     * 使用生成自动修改
     * @param updateWrapper
     * @return
     */
    public boolean base_list_update_by_id(UpdateWrapper<E> updateWrapper){
        boolean update = ((IService) sService).update(updateWrapper);
        return update;
    }

    /**
     * 逻辑删除
     * @param idlist id列表
     * @return
     */
    public boolean base_list_false_delete_by_id(List idlist){
//        UpdateWrapper<T> tUpdateWrapper = new UpdateWrapper<>();
//        tUpdateWrapper.eq(true,"flag",0);
        boolean b = ((IService) sService).removeByIds(idlist);
        return b;
    }

    /**
     *
     * @param pageindex 那一夜
     * @param pagesize 每页长度
     * @param ascs 正序字段,分割可多个
     * @param desc 正序字段,分割可多个
     * @return
     */
    public IPage<E> base_get_list_by_page(long pageindex,long pagesize,String ascs,String desc){

        String[] ascs_array=null;
        String[] desc_array=null;
        if(ascs!=null&&ascs.equals("")){
            ascs_array = ascs.split(",");
        }
        if(desc!=null&&desc.equals("")){
            desc_array = desc.split(",");
        }

        Page<E> pageinfo = new Page<>(pageindex,pagesize);
        if(ascs_array!=null){
            pageinfo.setAsc(ascs_array);
        }
        if(desc_array!=null){
            pageinfo.setDesc(desc_array);
        }
        IPage page = ((IService) sService).page(pageinfo,new QueryWrapper<E>());
        List records = page.getRecords();
        System.out.println(records);
        return page;
}


    /**
     *
     * @param queryWrapper 条件构造器
     * @param pageindex 那一夜
     * @param pagesize 每页长度
     * @param ascs 正序字段,分割可多个
     * @param desc 正序字段,分割可多个
     */
    public IPage<E> base_get_list_by_page(QueryWrapper queryWrapper,long pageindex,long pagesize,String ascs,String desc){
        String[] ascs_array=null;
        String[] desc_array=null;



        if(ascs!=null&&!ascs.equals("")){
            ascs_array = ascs.split(",");
        }
        if(desc!=null&&!desc.equals("")){
            desc_array = desc.split(",");
        }

        Page<E> pageinfo = new Page<>(pageindex,pagesize);
        if(ascs_array!=null){
            queryWrapper.orderByAsc(ascs_array);
        }
        if(desc_array!=null){
            queryWrapper.orderByDesc(desc_array);
        }
        IPage page = ((IService) sService).page(pageinfo,queryWrapper);
        return page;

    }
    public IPage<E> base_get_list_by_page(SymbolPlus symbolPlus){
       return this.base_get_list_by_page(symbolPlus.getQueryWrapper(),symbolPlus.getPageindex(),symbolPlus.getPagesize(),symbolPlus.getAscs(),symbolPlus.getDesc());
    }
    /**
     * 通用的对外方法
     * @param map
     * @return
     */
    public ResponseBean del_by_id( Map<String, String> map){

        boolean id = del(Integer.parseInt(map.get("id")));
        if(id){
            return  new ResponseBean(0, "Success", id);
        }
        return  new ResponseBean(803, "Specified object cannot be found", false);
    }

    public S getsService() {
        return sService;
    }

    public void setsService(S sService) {
        this.sService = sService;
    }
}
